/**
 * 初始化诊疗方案维护详情对话框
 */
var SdcTreatPlanInfoDlg = {
    treatPlanData:  {},
    /*validateFields: {
        itemName: {
            validators: {
                notEmpty: {
                    message: '方案名称不能为空'
                }
            }
        }
    }*/
};

/**
 * 选中事件
 */
SdcTreatPlanInfoDlg.check = function () {
    if ($("#id").val() == "0") {
        Sys.alert("请先创建诊疗方案！", 2);
        return false;
    }
    return true;
};

/**
 * 标签改变
 */
SdcTreatPlanInfoDlg.changeTabs = function (title, index) {
    switch (index) {
        case 1:
            if (SdcTreatPlanInfoDlg.check()) {
                SdcTreatPlanInfoDlg.initTimeQuantum();
            } else {
                $('#treatPlanTabs').tabs('select', 0);  //选中第一个页签
            }
            break;
        case 2:
            if (SdcTreatPlanInfoDlg.check()) {
                SdcTreatPlanInfoDlg.initPrecondition();
            } else {
                $('#treatPlanTabs').tabs('select', 0);  //选中第一个页签
            }
            break;
        default:
            break;
    };
};

/**
 *  初始化显示时间段
 */
SdcTreatPlanInfoDlg.initTimeQuantum = function (){
    SdcTreatTimeQuantum.init()
};
/**
 *  初始化前提
 */
SdcTreatPlanInfoDlg.initPrecondition = function (){

};

SdcTreatPlanInfoDlg.search = function () {
    SdcTreatPlanInfoDlg.initSourceForm();
};

SdcTreatPlanInfoDlg.clearData = function() {
    this.treatPlanData = {};
}

SdcTreatPlanInfoDlg.set = function(key, value) {
    this.treatPlanData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}

SdcTreatPlanInfoDlg.get = function(key) {
    return $("#" + key).val();
}

SdcTreatPlanInfoDlg.sourceChange = function(source) {
    var sourceId = source.value;
    $('#dataSourceItem').combobox("clear");
    //根据父级机构 查询子机构
    var ajax = new $ax(Sys.backendPath + "/sds/sdsqcformitem/getSourceFieldAndStandDic", function(res){
        var fieldMap = $.parseJSON(res.fieldMap);
        $('#dataSourceItem').combobox({
            data: fieldMap,
            valueField:'value',
            textField:'text',
            onLoadSuccess: function () {
                $(this).combobox("setValue", "");
            }
        });
    },function(data){
        Sys.error("查询失败!" + data.responseJSON.message + "!");
    });
    ajax.set({"sourceId":sourceId});
    ajax.setAsync(false);
    ajax.start();
}

SdcTreatPlanInfoDlg.officeChange = function(that){
    var ajax = new $ax(Sys.backendPath + "/sdc/sdcqcconfig/getByEntityId", function(data){
        $('#qcId').combobox({
            data:data,
            valueField:'value',
            textField:'text',
            defaultFilter: 6,
            onLoadSuccess: function () { //加载完成后,val[0]写死设置选中第一项
                $(this).combobox("setValue", "");
            }
        });
    },function(data){
        layer.msg("查询失败!" + data.responseJSON.message + "!",{icon: 2});
    });
    ajax.set({"entityId":that.value});
    ajax.start();
}

SdcTreatPlanInfoDlg.switchChange = function(event,obj) {
    if (obj.value){
        $("#referenceFlag").show();
    }else {
        $("#referenceFlag").hide();
    }
}



SdcTreatPlanInfoDlg.collectData = function () {
    this.set('dataSource1',$('#dataSource1').combobox('getValue')).set('dataSourceItem1',$('#dataSourceItem1').combobox('getValue'))
        .set('dataSource2',$('#dataSource2').combobox('getValue')).set('dataSourceItem2',$('#dataSourceItem2').combobox('getValue'))
        .set('param1',encodeURIComponent($('#param1').val())).set('ruleMemo',$('#ruleMemo').val())
        .set('param2',encodeURIComponent($('#param2').val())).set('operator1',encodeURIComponent($('#operator1').val()))
        .set('operator2',encodeURIComponent($('#operator2').val()))
        .set('ruleDesc',$('#ruleDesc').val()).set('expCode',$("#expCode").val()).set('id',$("#id").val());
};


/**
 * 关闭此对话框
 */
SdcTreatPlanInfoDlg.close = function() {
    parent.layer.close(parent.layer.getFrameIndex(window.name)); //先得到当前iframe层的索引,再执行关闭
}

/**
 * 验证数据是否为空
 */
SdcTreatPlanInfoDlg.validate = function () {
    $('#sdcTreatPlanInfoForm').data("bootstrapValidator").resetForm();
    $('#sdcTreatPlanInfoForm').bootstrapValidator('validate');
    return $("#sdcTreatPlanInfoForm").data('bootstrapValidator').isValid();
}

/**
 * 提交新增
 */
SdcTreatPlanInfoDlg.addSubmit = function() {
    if (!this.validate()) {
        return;
    }

    var errinfo = "";
    var recommendType = Common_GetText('recommendType');
    var itemName      = Common_GetValue('itemName');
    var qcEntity      = Common_GetValue('qcEntity');
    var vteType = Common_GetValue('vteType')
    if(!recommendType){
        errinfo = errinfo + "方案推荐为空!<br>";
    }
    if (!itemName) {
        errinfo = errinfo + "方案名称为空!<br>";
    }
    if (!qcEntity) {
        errinfo = errinfo + "所属病种为空!<br>";
    }
    if (errinfo) {
        $.messager.alert("错误提示", errinfo, 'info');
        return;
    }

    var queryData = {};
    var ajax = new $ax(Sys.backendPath + "/sdc/sdctreatplan/updateTreatPlan", function (data) {
        //SdsDataConfig.initSourceForm();
        Sys.success("修改成功!");
        window.parent.SdcTreatPlan.table.refresh();
        $("#id").val(data.message)
    }, function (data) {
        Sys.error("修改失败!" + data.responseJSON.message + "!");
    });
    //ajax.set(this.qcRuleData);
    queryData['recommendType'] = recommendType;
    queryData['itemName'] = Common_GetValue('itemName');
    queryData['itemDesc'] = Common_GetValue('itemDesc');
    queryData['qcentityId'] = Common_GetValue('qcEntity');
    queryData['sourceFlag'] = Common_GetValue('sourceFlag')? 1 : 0;
    queryData['noCondition'] = Common_GetValue('noCondition')? 1 : 0;
    queryData['qcId'] = Common_GetValue('qcId');
    queryData['dataSource'] = Common_GetValue('dataSource');
    queryData['dataSourceItem'] = Common_GetValue('dataSourceItem');
    queryData['param'] = encodeURIComponent(Common_GetValue('param'));
    queryData['pushStart'] =Common_GetValue('pushStart');
    queryData['pushEnd'] = Common_GetValue('pushEnd');
    queryData['timeUnit'] = Common_GetValue('timeUnit');
    queryData['showPremise'] = Common_GetValue('showPremise');
    queryData['type'] = Common_GetValue('vteType');
    queryData['isActive'] = Common_GetValue('isActive')? 1 : 0;
    ajax.setData(queryData);
    ajax.start();
}

/**
 * 提交修改
 */
SdcTreatPlanInfoDlg.editSubmit = function() {

    if (!this.validate()) {
        return;
    }
    //提交信息
    Sys.ajaxForm('sdcTreatPlanInfoForm',Sys.backendPath + "/sdc/sdcqcconfig/updateRule",function(res) {
        Sys.success(res.message);
        window.parent.obj.qCChange();
        SdcQcRuleInfoDlg.close();
    });
}

function Common_GetText() {
    var itmValue = '';

    if (typeof(arguments[0]) !== 'string') return '';
    if (arguments[0] == '') return '';

    var $this = $('#'+ arguments[0]);
    if ($this.length < 1) return '';

    var className = $this.attr("class").split(' ')[0];

    if (className == 'textbox') {  //文本框
        itmValue = $this.val();
    }else if (className == 'hisui-numberbox') {  //数字
        itmValue = $this.val();
    }else if (className == 'hisui-datebox') {  //日期
        itmValue = $this.datebox('getValue');
    }else if (className == 'hisui-timespinner') {  //时间框
        itmValue = $this.timespinner('getValue');
    }else if (className == 'hisui-combobox') {  //下拉框
        itmValue = $this.combobox('getText');
    }else if (className == 'hisui-switchbox') {  //开关
        itmValue = $this.switchbox('getValue');
    }else if (className == 'hisui-checkbox') {  // 单个复选框
        itmValue = $this.attr("label");
    }else if (className == 'hisui-radio') {  //单个单选框
        itmValue =$this.attr("label");
    }else if (className == 'hisui-searchbox') {  //查询框框
        itmValue = $this.searchbox('getValue');
    }

    return itmValue;
}

function Common_GetValue() {
    var itmValue = '';

    if (typeof(arguments[0]) !== 'string') return '';
    if (arguments[0] == '') return '';

    var $this = $('#'+ arguments[0]);
    if ($this.length < 1) return '';

    var className = $this.attr("class").split(' ')[0];

    if (className == 'textbox') {  //文本框
        itmValue = $this.val();
    }else if (className == 'hisui-numberbox') {  //数字
        itmValue = $this.val();
    }else if (className == 'hisui-datebox') {  //日期
        itmValue = $this.datebox('getValue');
    }else if (className == 'hisui-timespinner') {  //时间框
        itmValue = $this.timespinner('getValue');
    }else if (className == 'hisui-combobox') {  //下拉框（多选下拉框没有封装）
        itmValue = $this.combobox('getValue');
    }else if (className == 'hisui-switchbox') {  //开关
        itmValue = $this.switchbox('getValue');
    }else if (className == 'hisui-checkbox') {  // 单个复选框
        itmValue = $this.checkbox('getValue');
    }else if (className == 'hisui-radio') {  //单个单选框
        itmValue = $this.radio('getValue');
    }else if (className == 'hisui-searchbox') {  //查询框框
        itmValue = $this.searchbox('getValue');
    }

    return itmValue;
}

SdcTreatPlanInfoDlg.initSourceForm = function (){
    var params = {};
    params.id = $("#id").val();
    params.recommendType = $("#recommendType").val();
    var ajax = new $ax(Sys.backendPath + "/sdc/sdctreatplan/treatPlan", function(data){
        if (data.dataSourceItem == ""){
            $("#referenceFlag").hide();
        } else {
            $("#referenceFlag").show();
        }

        $("#id").val(data.id);
        $("#qcEntity").combobox("select", data.qcentityId)
        $("#sourceFlag").checkbox('setValue',(data.sourceFlag) ? true : false);
        $("#noCondition").checkbox('setValue',(data.noCondition) ? true : false);
        $("#referenceEvent").switchbox('setValue',(data.dataSourceItem) ? true : false);
        $("#qcId").combobox("select", data.qcId)
        $("#dataSource").combobox("select", data.dataSource)
        $("#dataSourceItem").combobox("select", data.dataSourceItem);
        $("#isActive").checkbox('setValue',(data.sourceFlag) ? true : false);
        $("#param").val(data.param);
        $("#pushStart").val(data.pushStart);
        $("#pushEnd").val(data.pushEnd);
        $("#timeUnit").combobox("select", data.timeUnit)

        $("#itemName").val(data.itemName);
        $("#itemDesc").val(data.itemDesc);
    },function (data){
    });
    ajax.setData(params);
    ajax.start();

}

$(function() {
    Sys.initValidator("sdcTreatPlanInfoForm", SdcTreatPlanInfoDlg.validateFields);

    /*$HUI.combobox('#recommendType',
        {
            data:$.parseJSON($("#recommendTypeMap").val()),
            valueField:'value',
            textField:'text',
            defaultFilter: 6
        })*/

    $HUI.combobox('#timeUnit',
        {
            data:[
                {'value':'1','Desc':"年"},
                {'value':'2','Desc':"月"},
                {'value':'3','Desc':"日"},
                {'value':'4','Desc':"时"},
                {'value':'5','Desc':"分"},
                {'value':'6','Desc':"秒"},
            ],
            valueField:'value',
            textField:'Desc',
        })

    $HUI.combobox('#qcEntity',
        {
            data:$.parseJSON($("#qcEntityMap").val()),
            valueField:'value',
            textField:'text',
            defaultFilter: 6,
            onSelect:SdcTreatPlanInfoDlg.officeChange
        })


    /*$HUI.combobox('#qcId',
        {
            data:$.parseJSON($("#qcMap").val()),
            valueField:'value',
            textField:'text',
            defaultFilter: 6
        })*/


    $HUI.switchbox("#referenceEvent").setValue(false, SdcTreatPlanInfoDlg.switchChange);
    //SdcTreatPlanInfoDlg.initSourceForm();

    // 修改按钮默认显示数据源页签 新增按钮默认第一个页签
    if ($("#id").val() != "0") {
        $("#treatPlanTabs").tabs('select', 0);
    }
});
